import { Component, Input } from '@angular/core';
import { Http } from '@angular/http';
import { ActivatedRoute } from '@angular/router';

//Base components
import { OverviewModel } from '../../../shared/templates/overview/ratingOverview.model';
//Services
import { CompanyRiskService } from '../../companyRisk.service';
import { UtilsService } from '../../../shared/services/utils.service';

@Component({
    selector: 'mei-rating-overview',
    templateUrl: '../../../shared/templates/overview/ratingOverview.component.html',
    styleUrls: ['../../../shared/templates/overview/ratingOverview.component.css'],
    providers: [UtilsService]
})
export class RatingOverviewMeiComponent extends OverviewModel {
    private sub: any;

    @Input() set type(value: string) {
        this.meiType = value;
    }

    public manageablePercent: string;

    constructor(
        private route: ActivatedRoute,
        private riskService: CompanyRiskService,
        private http: Http) {
        super(http);
    }

    ngOnInit() {
        this.sub = this.route.params.subscribe(params => {
            this.load();
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
    }

    private load(): void {
        this.loaded = new Promise<void>((resolve, reject) => {
            //clusters
            this.getMeiClusters().then(clusters => {
                this.riskClusters = clusters;
                this.riskService.company.then(company => {
                    this.riskService.mei.then(mei => {
                        this.currentClusterCode = mei.assessmentCode;

                        var cluster = this.riskClusters.filter(function (cluster) {
                            return cluster.code == mei.assessmentCode;
                        })[0];

                        this.currentClusterName = cluster != undefined ? cluster.name : "N/A";

                        //rating chart
                        this.riskRating = UtilsService.roundUp(mei.riskRating);

                        //risk framework chart
                        this.riskFramework = [{
                            name: "Exposure",
                            open: 0,
                            close: mei.exposure,
                            labelValue: mei.exposure,
                            color: "#B3D4D1"
                        }, {
                            name: "Managed Risk",
                            open: 0,
                            close: mei.managedRisk,
                            labelValue: mei.managedRisk,
                            color: "#A7A9AC"
                        },
                        {
                            name: "Unmanaged Risk",
                            open: mei.managedRisk,
                            close: mei.exposure,
                            labelValue: mei.unmanagedRisk,
                            color: "#000000"
                        }];

                        if (this.meiType !== "IdiosyncraticIssue") {
                            //issue exposure chart - for baseline, this is ignored
                            if (this.meiType == 'MaterialIssue') {
                                var relativeExposure = UtilsService.roundUp(mei.exposure - mei.subindustryExposureScore);
                                this.riskIssueExposure = [{
                                    name: "Exposure",
                                    isSummary: false,
                                    // description: "(" + company.subPeerGroup + ")",
                                    description: 'Subindustry Exposure',
                                    open: 0,
                                    close: mei.subindustryExposureScore,
                                    labelValue: mei.subindustryExposureScore.toFixed(1),
                                    color: "#B3D5D1"
                                }, {
                                    name: "Exposure ", //has space because amchart uses this field as id for trendlines
                                    isSummary: false,
                                    // description: "(" + company.companyName + ")",
                                    description: company.companyName,
                                    boldDescription: true,
                                    open: 0,
                                    close: mei.exposure,
                                    labelValue: mei.exposure.toFixed(1),
                                    color: "#B3D5D1"
                                },
                                {
                                    name: "exposure", //id purpose for amcharts
                                    isSummary: true,
                                    description: 'Relative Exposure - SubIndustry',
                                    open: mei.subindustryExposureScore,
                                    close: mei.exposure,
                                    labelValue: relativeExposure > 0 ? "+" + relativeExposure : relativeExposure,
                                    color: "#68AAA3"
                                }];

                                this.riskIssueExposureSummary = [
                                    /*
                                    {
                                        name: "Relative Exposure - SubIndustry",
                                        value: relativeExposure > 0 ? "+" + relativeExposure : relativeExposure
                                    },
                                    */
                                    {
                                        name: "Excess Exposure - SubIndustry",
                                        value: ((mei.finalBetaFactor - 1) * 100).toFixed(0) + "%"
                                    },
                                    {
                                        name: "Beta",
                                        value: mei.finalBetaFactor % 1 == 0 ? mei.finalBetaFactor : mei.finalBetaFactor.toFixed(2)
                                    }
                                ];
                            }

                            //management chart
                            this.manageablePercent = ((mei.manageableRisk * 100) / mei.exposure).toFixed(0) + "%";
                            this.riskManagement = [{
                                name: "Exposure",
                                labelValue: mei.exposure,
                                value: 100,
                                color: "#B3D5D1",
                                className: '',
                            },
                            {
                                name: "Manageable Risk",
                                labelValue: mei.manageableRisk,
                                value: Math.round((mei.manageableRisk * 100) / mei.exposure),
                                color: "#B3D5D1",
                                label: this.manageablePercent,
                                className: ''
                            },
                            {
                                name: "Managed Risk",
                                labelValue: mei.managedRisk,
                                value: Math.round((mei.managedRisk * 100) / mei.exposure),
                                color: "#A7A9AC",
                                label: UtilsService.roundUp(mei.management) + '%',
                                className: 'boldText'
                            }];
                        }
                        resolve();
                    });
                });
            });
        });
    }
}